﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;

namespace YJKRevitRampModule.Forms
{
	public partial class SubsectionForm : System.Windows.Forms.Form
	{
		public SubsectionForm(int nTotalLength)
		{
			this.InitializeComponent();
			this.m_lstSubLength = new List<int>();
			this.m_nTotalLength = nTotalLength;
			this.TotalLength.Text = string.Format("总长度：{0}", nTotalLength);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			int num = 0;
			foreach (int num2 in this.m_lstSubLength)
			{
				if (num2 <= 0)
				{
					YJKMessageBox.Show("坡段长度必须大于零！！");
					return;
				}
				num += num2;
			}
			if (num != this.m_nTotalLength)
			{
				YJKMessageBox.Show("各段投影长度之和与总长度不符！");
				return;
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void SubsectionForm_Load(object sender, EventArgs e)
		{
			this.InitGridData();
		}

		private void InitGridData()
		{
			this.m_lstSubLength.Clear();
			this.dgvSegmentSInfo.Rows.Clear();
			int num = this.m_nTotalLength / this.m_nSectionNum;
			for (int i = 0; i < this.m_nSectionNum; i++)
			{
				if (i == this.m_nSectionNum - 1)
				{
					num = this.m_nTotalLength - i * num;
				}
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = (i + 1).ToString();
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = num.ToString();
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				this.m_lstSubLength.Add(num);
				this.dgvSegmentSInfo.Rows.Add(dataGridViewRow);
			}
		}

		private void cmbSectionNum_TextChanged(object sender, EventArgs e)
		{
			int num = 0;
			try
			{
				num = int.Parse(this.cmbSectionNum.Text);
				if (num < 1)
				{
					num = this.m_nSectionNum;
					this.cmbSectionNum.Text = num.ToString();
				}
			}
			catch
			{
				num = this.m_nSectionNum;
				this.cmbSectionNum.Text = num.ToString();
			}
			if (this.m_nSectionNum != num)
			{
				this.m_nSectionNum = num;
				this.InitGridData();
			}
		}

		private void dgvSegmentSInfo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvSegmentSInfo.CurrentCell.ColumnIndex == 1)
			{
				System.Windows.Forms.TextBox textBox = e.Control as System.Windows.Forms.TextBox;
				if (textBox != null)
				{
					textBox.TextChanged += this.DGVTextBoxTextChanged;
				}
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dgvSegmentSInfo.CurrentCell.RowIndex;
			string text = ((System.Windows.Forms.TextBox)sender).Text;
			if (this.dgvSegmentSInfo.CurrentCell.ColumnIndex == 1)
			{
				try
				{
					int value = int.Parse(text);
					this.m_lstSubLength[rowIndex] = value;
				}
				catch
				{
					((System.Windows.Forms.TextBox)sender).Text = this.m_lstSubLength[rowIndex].ToString();
				}
			}
		}

		private int m_nSectionNum = 1;

		private int m_nTotalLength;

		public List<int> m_lstSubLength;
	}
}
